﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HCY.CTP.DBShangZheng;
using IFServerDLL.DB;
using IFClientDLL.MyCalc.Data.Line;
using IFClientDLL.MyCalc.Data.Candle;

namespace IFClientDLL.MyCalc
{
    public class MyCalcData
    {
        MyData_IF _myif;
        MyData_Candle_Minute _candle;
        MyData_Line_Minute _line;
        MyData_DaPan _DaPan;
        public MyData_DaPan LastData_DaPan { get { return _DaPan; } }
        MyData_RedGreen _RedGreen;
        public MyData_RedGreen LastData_RedGreen { get { return _RedGreen; } }
        private ITryIF _tryIF;
        public MyCalcData(ITryIF tryif)
        {
            _tryIF = tryif;
            tryif.DataServer.OnReceiveIFData += new Action<MyData_IF>(tryif_OnReceiveData);
            tryif.DataServer.OnReceiveMarketData += new Action<MyData_DaPan>(DataServer_OnReceiveMarketData);
            tryif.DataServer.OnReceiveRedGreenBarData += new Action<MyData_RedGreen>(DataServer_OnReceiveRedGreenBarData);
        }

        void DataServer_OnReceiveRedGreenBarData(MyData_RedGreen obj)
        {
            _RedGreen = obj;
        }
        void DataServer_OnReceiveMarketData(MyData_DaPan obj)
        {
            _DaPan = obj;
        }

        void tryif_OnReceiveData(MyData_IF obj)
        {
            _myif = obj;
            if (_line == null)
            {
                _line = new MyData_Line_Minute(_myif, _line);
                _candle = new MyData_Candle_Minute(_myif, _candle);
            }
            else
            {
                var minuteTime = new DateTime(obj.MarketTime.Year, obj.MarketTime.Month, obj.MarketTime.Day, obj.MarketTime.Hour, obj.MarketTime.Minute, 0);
                if (_line.MinuteTime.CompareTo(minuteTime) == 0)
                {
                    _line.AddData(_myif);
                    _candle.AddData(_myif);
                }
                else
                {
                    _line = new MyData_Line_Minute(_myif, _line);
                    _candle = new MyData_Candle_Minute(_myif, _candle);
                }
            }
            if (OnTickChanged != null)
            {
                OnTickChanged(_myif);
            }
            if (OnCandleChanged_Minute != null)
            {
                OnCandleChanged_Minute(_candle);
            }
            if (OnLineChanged_Minute != null)
            {
                OnLineChanged_Minute(_line);
            }
        }
        public event Action<MyData_IF> OnTickChanged;
        public event Action<MyData_Line_Minute> OnLineChanged_Minute;
        public event Action<MyData_Candle_Minute> OnCandleChanged_Minute;
    }
}
